package com.springcloud.travel.meals.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spingcloud.travel.common.TableResult;
import com.springcloud.travel.entity.Meals;
import com.springcloud.travel.meals.service.MealsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/sys/meals")
public class MealsController {
    @Autowired
    private MealsService mealsService;

    @RequestMapping("/table")
    public TableResult getTable(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "5") Integer limit) {
        System.out.println("table controller");
        EntityWrapper wrapper = new EntityWrapper();
        PageHelper.startPage(page, limit);
        List list = mealsService.selectList(wrapper);
        PageInfo pageInfo = new PageInfo(list);
        return TableResult.getSuccess((int) pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping("/add")
    public Boolean addMeals(@RequestBody Meals meals) {
        try {
            boolean insert = mealsService.insert(meals);
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    @RequestMapping("/update")
    public Boolean updateMeals(@RequestBody Meals meals) {
        try {
            boolean b = mealsService.updateById(meals);
            return b;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    @RequestMapping("/del")
    public Boolean delMeals(@RequestParam("ids") String[] ids) {
        try {
            mealsService.deleteBatchIds(Arrays.asList(ids));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
